<?php

namespace App\Http\Controllers\Home;

use App\Models\DiningReserve;
use App\Models\DiningReserveProd;
use App\Models\ProdOrder;
use App\Models\Recharge;
use App\Models\User;
use App\Models\UserAddress;
use App\Models\Withdraw;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;

/**
 * @group 前台接口文档
 * @title 订单模块接口
 * @author chenke
 * @description 酒庄前台订单板块接口
 * @header name:Token type:string require:1 desc:登录令牌
 * @header name:Lang type:string require:1 desc:语言类型(en或zn)
 *  */
class Order extends Base
{
    /**
     * @title 商品订单添加
     * @description 添加商品订单数据
     * @url /api/order/creord
     * @method POST
     * @module 订单板块
     * @author chenke
     * @param name:proid type:int require:1 default: other: desc:商品ID
     * @param name:proprice type:int require:1 default: other: desc:商品成交价
     * @param name:pronum type:int require:1 default: other: desc:商品数量
     * @param name:youhui type:int require:1 default:0 other: desc:优惠金额
     * @param name:addressid type:int require:1 default:0 other: desc:地址ID
     * @param name:fee type:int require:1 default:0 other: desc:运费，为0时代表包邮
     * @param name:sendtype type:string require:1 default: other: desc:配送方式
     * @param name:remark type:string require:1 default: other: desc:订单备注
     * @return code:错误码
     * @return message:错误提示
     * @return data: 订单信息@
     * @data id:订单ID uid:用户ID
     */
    public function orderAdd(Request $request){
        $lang = $request->header('Lang')?$request->header('Lang'):'zn';
        try {
            $data = $request->post();
            $rules = [
                'youhui'=>'required|integer',
                'addressid'=>'required|integer',
                'fee'=>'required|integer',
                'proid'=>'required|integer',
                'proprice'=>'required|integer',
                'pronum'=>'required|integer',
                'remark'=>'string',
                'sendtype'=>'required|string'
            ];
            $messages = [
                'addressid.required'=>  $this->lang['REQUIRED'][$lang],
                'fee.required'=>  $this->lang['REQUIRED'][$lang],
                'prods.required'=>  $this->lang['REQUIRED'][$lang],
                'youhui.required'=>  $this->lang['REQUIRED'][$lang],
                'proprice.required'=> $this->lang['REQUIRED'][$lang],
                'sendtype.required'=>  $this->lang['REQUIRED'][$lang],
                'proprice.integer'=>  $this->lang['INTEGER'][$lang],
                'addressid.integer'=> $this->lang['INTEGER'][$lang],
                'fee.integer'=> $this->lang['INTEGER'][$lang],
                'proid.integer'=> $this->lang['INTEGER'][$lang],
                'pronum.integer'=> $this->lang['INTEGER'][$lang],
                'youhui.integer'=> $this->lang['INTEGER'][$lang]
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $data['ordtype'] = 1;
            $data['remark'] = (isset($data['remark'])&&$data['remark'])?$data['remark']:'';
            $result =  (new ProdOrder())->oneInsert($data,$request->token,$lang);
            if($result['code']){
                $this->logErr($result['code'],$result['message'],'orderAdd');
            }
            return response()->json($result);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 积分商品订单添加
     * @description 添加积分商品订单数据
     * @url /api/order/crejord
     * @method POST
     * @module 订单板块
     * @author chenke
     * @param name:proid type:int require:1 default: other: desc:商品ID
     * @param name:pronum type:int require:1 default: other: desc:商品数量
     * @param name:addressid type:int require:1 default:0 other: desc:地址ID
     * @param name:fee type:int require:1 default:0 other: desc:运费，为0时代表包邮
     * @param name:sendtype type:string require:1 default: other: desc:配送方式
     * @param name:remark type:string require:1 default: other: desc:订单备注
     * @return code:错误码
     * @return message:错误提示
     * @return data: 订单信息@
     * @data id:订单ID uid:用户ID moneys:交易金额 coins:交易数量
     */
    public function jifenOrderAdd(Request $request){
        $lang = $request->header('Lang')?$request->header('Lang'):'zn';
        try {
            $data = $request->post();
            $rules = [
                'addressid'=>'required|integer',
                'fee'=>'required|integer',
                'proid'=>'required|integer',
                'pronum'=>'required|integer',
                'remark'=>'string',
                'sendtype'=>'required|string'
            ];
            $messages = [
                'addressid.required'=>  $this->lang['REQUIRED'][$lang],
                'fee.required'=>  $this->lang['REQUIRED'][$lang],
                'prods.required'=>  $this->lang['REQUIRED'][$lang],
                'sendtype.required'=>  $this->lang['REQUIRED'][$lang],
                'addressid.integer'=> $this->lang['INTEGER'][$lang],
                'fee.integer'=> $this->lang['INTEGER'][$lang],
                'proid.integer'=> $this->lang['INTEGER'][$lang],
                'pronum.integer'=> $this->lang['INTEGER'][$lang]
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $data['ordtype'] = 2;
            $data['remark'] = (isset($data['remark'])&&$data['remark'])?$data['remark']:'';
            $result =  (new ProdOrder())->insertJifen($data,$request->token,$lang);
            if($result['code']){
                $this->logErr($result['code'],$result['message'],'orderAdd');
            }
            return response()->json($result);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 商品/积分商品订单删除
     * @description 前台商品/积分商品订单删除
     * @url /api/order/delprodord
     * @method POST
     * @module 商品板块
     * @author chenke
     * @param name:id type:int require:1 default: other: desc:订单ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:成功时为空
     */
    public function prodOrderDelete(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->post();
            $rules = [
                'id'=>'required|integer',
            ];
            $messages = [
                'id.required'=> $this->lang['REQUIRED'][$lang],
                'id.integer'=> $this->lang['INTEGER'][$lang]
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if(!$user){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            $res =  (new ProdOrder())->where('id',$data['id'])->update(['deleted_at'=>date('Y-m-d H:i:s')]);
            if($res){
                return response()->json([
                    'code'=>0,
                    'data'=>''
                ]);
            }else{
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['ADD_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 商品/积分商品订单确认收货
     * @description 前台商品/积分商品订单确认收货
     * @url /api/order/consend
     * @method POST
     * @module 商品板块
     * @author chenke
     * @param name:id type:int require:1 default: other: desc:订单ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:成功时为空
     */
    public function prodOrderSendConfirm(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->post();
            $rules = [
                'id'=>'required|integer',
            ];
            $messages = [
                'id.required'=> $this->lang['REQUIRED'][$lang],
                'id.integer'=> $this->lang['INTEGER'][$lang]
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if(!$user){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            $order =  (new ProdOrder())->where('id',$data['id'])->first();
            if($order){
                if($order->status ==3 ){
                    $order->status  = 4;
                    $order->recivetime  = date('Y-m-d H:i:s');
                    $order->save();
                    return response()->json([
                        'code'=>0,
                        'data'=>''
                    ]);
                }else{
                    return response()->json([
                        'code'=>1,
                        'message'=>$this->lang['OPERATE_ERR'][$lang]
                    ]);
                }
            }else{
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['ORDER_UNKOWN'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 用户地址管理列表
     * @description 前台用户地址管理列表
     * @url /api/order/adrshow
     * @method GET
     * @module 用户地址板块
     * @author chenke
     * @param name:uid type:int require:1 default:0 other: desc:用户ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:地址数据@
     * @data id:地址ID username:接收人姓名 contact:接收电话 address:接收地址 isdefault:是否默认
     */
    public function addressList(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $id = $request->get('uid');
            if(!$id){
                return response()->json([
                   'code'=>1,
                   'message'=>'用户不存在'
                ]);
            }
            $data = DB::table('user_addresses')->where('uid',$id)->get()->toArray();
            return response()->json([
                'code'=>0,
                'message'=>$data
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 用户地址添加
     * @description 前台用户添加地址
     * @url /api/order/creadr
     * @method POST
     * @module 用户地址板块
     * @author chenke
     * @param name:username type:string require:1 default: other: desc:用户姓名
     * @param name:contact type:string require:1 default: other: desc:联系电话
     * @param name:address type:string require:1 default: other: desc:收货地址
     * @param name:isdefault type:int require:1 default:0 other: desc:是否默认 1为默认
     * @return code:错误码
     * @return message:错误提示
     * @return data:地址ID
     */
    public function addressAdd(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->post();
            $rules = [
                'username'=>'required|string',
                'contact'=>'required|string',
                'address'=>'required|string',
                'isdefault'=>'required|integer'
            ];
            $messages = [
                'username.required'=> $this->lang['REQUIRED'][$lang],
                'contact.required'=> $this->lang['REQUIRED'][$lang],
                'address.required'=> $this->lang['REQUIRED'][$lang],
                'isdefault.required'=> $this->lang['REQUIRED'][$lang],
                'isdefault.integer'=> $this->lang['INTEGER'][$lang]
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $model = new UserAddress();
            $model->username = $data['username'];
            $model->contact = $data['contact'];
            $model->address = $data['address'];
            $model->uid = $user->id;
            $model->isdefault = $data['isdefault']?1:0;
            if($data['isdefault']==1){
                (new UserAddress())->where('uid',$user->id)->update(['isdefault'=>0,'updated_at'=>date('Y-m-d H:i:s')]);
            }
            $res =  $model->save();
            if($res){
                return response()->json([
                    'code'=>0,
                    'data'=>$model->id
                ]);
            }else{
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['ADD_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 用户地址编辑
     * @description 前台用户编辑地址
     * @url /api/order/edtadr
     * @method POST
     * @module 用户地址板块
     * @author chenke
     * @param name:id type:string require:1 default: other: desc:地址
     * @param name:username type:string require:1 default: other: desc:用户姓名
     * @param name:contact type:string require:1 default: other: desc:联系电话
     * @param name:address type:string require:1 default: other: desc:收货地址
     * @param name:isdefault type:int require:1 default:0 other: desc:是否默认 1为默认
     * @return code:错误码
     * @return message:错误提示
     * @return data:地址ID
     */
    public function addressEdit(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->post();
            $rules = [
                'id'=>'required|integer',
                'username'=>'required|string',
                'contact'=>'required|string',
                'address'=>'required|string',
                'isdefault'=>'required|integer'

            ];
            $messages = [
                'username.required'=> $this->lang['REQUIRED'][$lang],
                'contact.required'=> $this->lang['REQUIRED'][$lang],
                'address.required'=> $this->lang['REQUIRED'][$lang],
                'id.required'=> $this->lang['REQUIRED'][$lang],
                'id.integer'=> $this->lang['INTEGER'][$lang],
                'isdefault.required'=> $this->lang['REQUIRED'][$lang],
                'isdefault.integer'=> $this->lang['INTEGER'][$lang]
            ];

            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $model = UserAddress::find($data['id']);
            $model->username = $data['username'];
            $model->contact = $data['contact'];
            $model->address = $data['address'];
            $model->uid = $user->id;
            $model->isdefault = $data['isdefault'];
            if($data['isdefault']==1){
                (new UserAddress())->where('uid',$user->id)->update(['isdefault'=>0,'updated_at'=>date('Y-m-d H:i:s')]);
            }
            $res =  $model->save();
            if($res){
                return response()->json([
                    'code'=>0,
                    'data'=>$data['id']
                ]);
            }else{
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['ADD_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 用户地址删除
     * @description 前台用户删除地址
     * @url /api/order/deladr
     * @method POST
     * @module 用户地址板块
     * @author chenke
     * @param name:id type:int require:1 default: other: desc:地址ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:成功时为空
     */
    public function addressDelete(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->post();
            $rules = [
                'id'=>'required|integer'
            ];
            $messages = [
                'id.required'=> $this->lang['REQUIRED'][$lang],
                'id.integer'=> $this->lang['INTEGER'][$lang],
            ];

            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $res =  (new UserAddress())->where('id',$data['id'])->delete();
            if($res){
                return response()->json([
                    'code'=>0,
                    'data'=>''
                ]);
            }else{
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['ADD_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 添加餐厅订单
     * @description 添加餐厅订单数据
     * @url /api/order/credinord
     * @method POST
     * @module 餐厅订单板块
     * @author chenke
     * @param name:ordtype type:int require:1 default: other: desc:(预定类型1预定2现场点单)
     * @param name:tid type:int require:1 default: other: desc:桌子ID
     * @param name:timetype type:int require:1 default: other: desc:就餐时间段(1,2,3,4四个时间段)
     * @param name:paytye type:int require:1 default: other: desc:支付方式(1为钱包支付、2为反馈钱包支付)
     * @param name:total type:int require:1 default: other: desc:订单总金额
     * @param name:earnest type:int require:1 default: other: desc:订单订金
     * @param name:sitday type:date require:1 default:0 other: desc:就餐日期
     * @param name:pernum type:int require:1 default:0 other: desc:就餐人数
     * @param name:prods type:json require:0 default: other: desc:(商品数据[{proid:1,proprice:180,pronum:5},{proid:2,proprice:300,pronum:2}])
     * @param name:remark type:string require:0 default: other: desc:订单备注
     * @return code:错误码
     * @return message:错误提示
     * @return data: 订单信息@
     * @data id:订单ID token:用户新token
     */
    public function diniOrderSubmit(Request $request){
        try {
            $data = $request->post();
            $rules = [
                'tid'=>'required|integer',
                'ordtype'=>[
                    'required',Rule::in([1,2])
                ],
                'timetype'=>[
                    'required',Rule::in([1,2,3,4])
                ],
                'paytye'=>[
                    'required',Rule::in([1,2])
                ],
                'pernum'=>'required|integer',
                'total'=>'required|integer|min:0',
                'earnest'=>'required|integer|min:0',
                'remark'=>'string',
                'sitday'=>'required|date',
                'prods'=>'json'
            ];
            $messages = [
                'tid.required'=> $this->lang['REQUIRED'][$request->lang],
                'ordtype.required'=> $this->lang['REQUIRED'][$request->lang],
                'timetype.required'=>  $this->lang['REQUIRED'][$request->lang],
                'pernum.required'=>  $this->lang['REQUIRED'][$request->lang],
                'sitday.required'=>  $this->lang['REQUIRED'][$request->lang],
                'total.required'=>  $this->lang['REQUIRED'][$request->lang],
                'earnest.required'=>  $this->lang['REQUIRED'][$request->lang],
                'tid.integer'=> $this->lang['INTEGER'][$request->lang],
                'total.integer'=> $this->lang['INTEGER'][$request->lang],
                'total.min'=> $this->lang['MIN'][$request->lang],
                'earnest.integer'=> $this->lang['INTEGER'][$request->lang],
                'earnest.min'=> $this->lang['MIN'][$request->lang],
                'timetype.in'=> $this->lang['IN'][$request->lang],
                'paytye.in'=> $this->lang['IN'][$request->lang],
                'pernum.integer'=> $this->lang['INTEGER'][$request->lang],
                'sitday.date'=>  $this->lang['DATE'][$request->lang],
                'prods.json'=>  $this->lang['JSON'][$request->lang]
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $data['remark'] = (isset($data['remark'])&&$data['remark'])?$data['remark']:'';
//            return response()->json($data);
            $result =  (new DiningReserve())->insertReserve($data,$request->token,$request->lang);
            if($result['code']){
                $this->logErr($result['code'],$result['message'],'diniOrderAdd');
            }
            return response()->json($result);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$e->getMessage() //$this->lang['ERROR'][$request->lang]
            ]);
        }
    }

    /**
     * @title 获取用户商品订单
     * @description 前台获取用户商品订单
     * @url /api/order/getord
     * @method GET
     * @module 商品订单板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:status type:int require:1 default:0 other: desc:商品订单状态(0全部1待付款2待发货3待收货4已完成5已取消6已退)
     * @return code:错误码
     * @return message:错误提示
     * @return data:订单数据@
     * @data id:订单ID proname:产品英文名 proname_zn:产品中文名 image:产品图片 ordersn:订单编号 proprice:产品成交价 sendno:发货单号 pronum:商品购买数量 youhui:优惠 status:状态
     */
    public function prodOrderList(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->all();
            $limit = $data['limit'];
            $page = $data['page'];
            $uid = (new User())->where('token',$request->token)->value('id');
            if(!$uid){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            $where = [
                ['uid','=',$uid]
            ];
            $status = (isset($data['status'])&&$data['status'])?$data['status']:0;
            if($status){
                $where[]=['status','=',$status];
            }
            $model = (new ProdOrder())->leftJoin('products','products.id','=','prod_orders.proid')->where($where)
                    ->select('prod_orders.id','prod_orders.pronum','prod_orders.proprice','prod_orders.ordersn','prod_orders.proprice','prod_orders.sendno','prod_orders.youhui','prod_orders.status','products.id as proid','products.proname','products.proname_zn','products.image');
            $offset = ($page-1) * $limit;
            $total =  $model->count();  //获取模型总数据条数
            $result = $model->offset($offset)->limit($limit)->get()->toArray();
            $data = array(
                'total'=>$total,
                'list'=>$result
            );
            return response()->json( [
                'code'=>0,
                'data'=>$data
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$e->getMessage()// $this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 获取商品订单详情
     * @description 前台获取商品订单详情
     * @url /api/order/getordetail
     * @method GET
     * @module 商品订单板块
     * @author chenke
     * @param name:orderid type:int require:1 default:1 other: desc:订单ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:订单信息@
     * @data id:订单ID proname:产品英文名 proname_zn:产品中文名 image:产品图片 ordersn:订单编号 proprice:产品成交价 sendno:发货单号 pronum:商品购买数量 youhui:优惠 status:状态
     */
    public function prodOrderDetail(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->all();
            $orderid = $data['orderid'];
            $user = (new User())->where('token',$request->token)->first();
            if(!$user){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $order = (new ProdOrder())->leftJoin('user_addresses','user_addresses.id','=','prod_orders.addressid')
                    ->leftJoin('products','products.id','=','prod_orders.proid')->where([ ['prod_orders.id','=',$orderid],['prod_orders.uid','=',$user->id] ])
                    ->select('prod_orders.*','username','contact','address','proname','proname_zn','image')
                    ->first();
            if($order){
                return response()->json( [
                    'code'=>0,
                    'data'=>$order->toArray()
                ]);
            }else{
                return response()->json( [
                    'code'=>1,
                    'message'=>$this->lang['GET_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 获取积分商品订单详情
     * @description 前台获取商品订单详情
     * @url /api/order/jifendetail
     * @method GET
     * @module 商品订单板块
     * @author chenke
     * @param name:orderid type:int require:1 default:1 other: desc:订单ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:订单信息@
     * @data id:订单ID proname:产品英文名 proname_zn:产品中文名 image:产品图片 ordersn:订单编号 proprice:产品成交价 sendno:发货单号 pronum:商品购买数量 youhui:优惠 status:状态
     */
    public function jifenOrderDetail(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->all();
            $orderid = $data['orderid'];
            $user = (new User())->where('token',$request->token)->first();
            if(!$user){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $order = (new ProdOrder())->leftJoin('user_addresses','user_addresses.id','=','prod_orders.addressid')
                ->leftJoin('products','products.id','=','prod_orders.proid')->where([ ['prod_orders.id','=',$orderid],['prod_orders.uid','=',$user->id] ])
                ->select('prod_orders.*','username','contact','address','proname','proname_zn','image')
                ->first();
            if($order){
                return response()->json( [
                    'code'=>0,
                    'data'=>$order->toArray()
                ]);
            }else{
                return response()->json( [
                    'code'=>1,
                    'message'=>$this->lang['GET_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 获取餐厅订单详情
     * @description 前台获取餐厅订单详情
     * @url /api/order/dinidetail
     * @method GET
     * @module 餐厅订单板块
     * @author chenke
     * @param name:orderid type:int require:1 default:1 other: desc:订单ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:订单信息@
     * @data id:订单ID proname:产品英文名 proname_zn:产品中文名 image:产品图片 ordersn:订单编 pronum:商品购买数量 status:状态
     */
    public function DiniOrderDetail(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->all();
            $orderid = $data['orderid'];
            $user = (new User())->where('token',$request->token)->first();
            if(!$user){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $order = (new DiningReserve())->where([ ['id','=',$orderid],['uid','=',$user->id] ])->first();
            $prods = (new DiningReserveProd())->leftJoin('products','products.id','=','dining_reserve_prods.proid')
                    ->where('did',$orderid)->select('dining_reserve_prods.*','proname','proname_zn','image')->get();
            $return = $order->toArray();
            if($prods){
                $return['prods'] = $prods->toArray();
            }else{
                $return['prods'] = [];
            }
            if($order){
                return response()->json( [
                    'code'=>0,
                    'data'=>$return
                ]);
            }else{
                return response()->json( [
                    'code'=>1,
                    'message'=>$this->lang['GET_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 充值订单详情
     * @description 前台充值订单详情
     * @url /api/order/redetail
     * @method GET
     * @module 充值板块
     * @author chenke
     * @param name:orderid type:int require:1 default:1 other: desc:订单ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:订单信息@
     * @data id:订单ID proname:产品英文名 proname_zn:产品中文名 image:产品图片 ordersn:订单编 pronum:商品购买数量 status:状态
     */
    public function rechargeDetail(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->all();
            $orderid = $data['orderid'];
            $user = (new User())->where('token',$request->token)->first();
            if(!$user){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $order = (new Recharge())->where([ ['id','=',$orderid],['uid','=',$user->id] ])->first();
            if($order){
                return response()->json( [
                    'code'=>0,
                    'data'=>$order->toArray()
                ]);
            }else{
                return response()->json( [
                    'code'=>1,
                    'message'=>$this->lang['GET_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 提现订单详情
     * @description 前台提现订单详情
     * @url /api/order/wddetail
     * @method GET
     * @module 提现板块
     * @author chenke
     * @param name:orderid type:int require:1 default:1 other: desc:订单ID
     * @return code:错误码
     * @return message:错误提示
     * @return data:订单信息@
     * @data id:订单ID proname:产品英文名 proname_zn:产品中文名 image:产品图片 ordersn:订单编 pronum:商品购买数量 status:状态
     */
    public function withDrawDetail(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->all();
            $orderid = $data['orderid'];
            $user = (new User())->where('token',$request->token)->first();
            if(!$user){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $order = (new Withdraw())->where([ ['id','=',$orderid],['uid','=',$user->id] ])->first();
            if($order){
                return response()->json( [
                    'code'=>0,
                    'data'=>$order->toArray()
                ]);
            }else{
                return response()->json( [
                    'code'=>1,
                    'message'=>$this->lang['GET_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }


}
